package com.example.wyc.mybiye;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.wyc.mybiye1.Main2Activity;

public class MainActivity extends AppCompatActivity {
    public TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        daoJiShi.start();
    }

    public void init(){
        textView=findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, Main2Activity.class);
                isDaoJiShi=false;
                finish();
                startActivity(intent);
            }
        });
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    textView.setText("点击进入 : "+msg.obj.toString());
                    break;
                case 2:
                    textView.setText("点击进入 : "+msg.obj.toString());
                    Intent intent=new Intent(MainActivity.this, Main2Activity.class);
                    isDaoJiShi=false;
                    finish();
                    startActivity(intent);
                    break;
            }
        }
    };
    boolean isDaoJiShi=true;
    Thread daoJiShi=new Thread(new Runnable() {
        @Override
        public void run() {
            int i=5;
            while (i>0) {
                if (isDaoJiShi) {
                    Message msg = new Message();
                    YanChi(1000);
                    if (i != 1) {
                        msg.what = 1;
                        msg.obj = i;
                        handler.sendMessage(msg);
                    } else {
                        msg.what = 2;
                        msg.obj = i;
                        handler.sendMessage(msg);
                    }
                    i--;
                }else {
                    return;
                }
            }
        }
    });
    //延迟时间
    public void YanChi(int i){
        try {
            Thread.sleep(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
